package g11.action;

import g11.actionutil.VisitationContentUtil;
import g11.actionutil.VisitationContentUtilImpl;
import g11.actionutil.VisitationUtil;
import g11.actionutil.VisitationUtilImpl;
import g11.dao.AppointmentDAO;
import g11.dao.AppointmentDAOImpl;
import g11.dao.DAOException;
import g11.dao.DrugDAO;
import g11.dao.DrugDAOImpl;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.dao.PatientDAO;
import g11.dao.PatientDAOImpl;
import g11.dao.VisitationContentDAO;
import g11.dao.VisitationContentDAOImpl;
import g11.dao.VisitationDAO;
import g11.dao.VisitationDAOImpl;
import g11.model.Appointment;
import g11.model.Drug;
import g11.model.Employee;
import g11.model.Patient;
import g11.model.Prescription;
import g11.model.Visitation;
import g11.model.VisitationContent;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class VisitationAction extends ActionSupport implements SessionAware,
		RequestAware {

	private Map<String, Object> session;
	private List<VisitationContent> retrievedVisitationContents;
	private Integer selectedPatientId;
	private Patient selectedPatient;
	private Integer selectedVisitationId;
	private VisitationContent retrievedVisitationContent;
	private Set<Prescription> prescriptionList;
	private Drug drug;
	private ArrayList<String> drugId;
	private ArrayList<String> dosage;
	// used when show all visitation list
	private String currentEmployeeId;

	// actionMode is used to distinguish different action, for exemple:
	// "showPaitent":show a patient's visitation
	// "showAll":show all visitation list of a doctor,
	// "create":create visitation
	// "update": update visitation, etc
	private String actionMode;
	private Integer selectedAppointmentId;
	private Appointment selectedAppointment;// used when create new visitation
											// from appointment
	private List<Drug> retrievedDrugs;
	private Map<String, String> selectedDrugs;// key:id, value: dosage

	private String searchContent;
	private String searchType;
	private Integer searchDrugId;
	private String startDate;
	private String endDate;
	private Integer selectedVisitationContentId;
	private Integer searchDoctorId;

	private List<Employee> doctorList;

	private Map<Patient, Integer> patientStatMap;

	private Integer patientCount;

	private java.sql.Date displayStartDate;
	private java.sql.Date displayEndDate;

	private Integer patientId;

	public Integer getPatientId() {
		return patientId;
	}

	public void setPatientId(Integer patientId) {
		this.patientId = patientId;
	}

	public java.util.Date getDisplayStartDate() {
		return displayStartDate;
	}

	public void setDisplayStartDate(java.sql.Date displayStartDate) {
		this.displayStartDate = displayStartDate;
	}

	public java.util.Date getDisplayEndDate() {
		return displayEndDate;
	}

	public void setDisplayEndDate(java.sql.Date displayEndDate) {
		this.displayEndDate = displayEndDate;
	}

	public Integer getPatientCount() {
		return patientCount;
	}

	public void setPatientCount(Integer patientCount) {
		this.patientCount = patientCount;
	}

	public Map<Patient, Integer> getPatientStatMap() {
		return patientStatMap;
	}

	public void setPatientStatMap(Map<Patient, Integer> patientStatMap) {
		this.patientStatMap = patientStatMap;
	}

	public Integer getSearchDoctorId() {
		return searchDoctorId;
	}

	public void setSearchDoctorId(Integer searchDoctorId) {
		this.searchDoctorId = searchDoctorId;
	}

	public List<Employee> getDoctorList() {
		return doctorList;
	}

	public void setDoctorList(List<Employee> doctorList) {
		this.doctorList = doctorList;
	}

	public Integer getSelectedVisitationContentId() {
		return selectedVisitationContentId;
	}

	public void setSelectedVisitationContentId(
			Integer selectedVisitationContentId) {
		this.selectedVisitationContentId = selectedVisitationContentId;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getSearchContent() {
		return searchContent;
	}

	public Integer getSearchDrugId() {
		return searchDrugId;
	}

	public void setSearchDrugId(Integer searchDrugId) {
		this.searchDrugId = searchDrugId;
	}

	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public ArrayList<String> getDrugId() {
		return drugId;
	}

	public void setDrugId(ArrayList<String> drugId) {
		this.drugId = drugId;
	}

	public ArrayList<String> getDosage() {
		return dosage;
	}

	public void setDosage(ArrayList<String> dosage) {
		this.dosage = dosage;
	}

	public Drug getDrug() {
		return drug;
	}

	public void setDrug(Drug drug) {
		this.drug = drug;
	}

	public Map<String, Object> getSession() {
		return session;
	}
	
	public String appendNewDrugForEditPage() {
		return SUCCESS;
	}

	public List<VisitationContent> getRetrievedVisitationContents() {
		return retrievedVisitationContents;
	}

	public void setRetrievedVisitationContents(
			List<VisitationContent> retrievedVisitationContents) {
		this.retrievedVisitationContents = retrievedVisitationContents;
	}

	public Integer getSelectedVisitationId() {
		return this.selectedVisitationId;
	}

	public void setSelectedVisitationId(Integer visitationId) {
		this.selectedVisitationId = visitationId;
	}

	public VisitationContent getRetrievedVisitationContent() {
		return retrievedVisitationContent;
	}

	public void setRetrievedVisitationContent(
			VisitationContent retrievedVisitationContent) {
		this.retrievedVisitationContent = retrievedVisitationContent;
	}

	public List<Drug> getRetrievedDrugs() {
		return this.retrievedDrugs;
	}

	public void setRetrievedDrugs(List<Drug> retrievedDrugs) {
		this.retrievedDrugs = retrievedDrugs;
	}

	public Patient getSelectedPatient() {
		return selectedPatient;
	}

	public void setSelectedPatient(Patient selectedPatient) {
		this.selectedPatient = selectedPatient;
	}

	public String getActionMode() {
		return actionMode;
	}

	public void setActionMode(String actionMode) {
		this.actionMode = actionMode;
	}

	public String getCurrentEmployeeId() {
		return currentEmployeeId;
	}

	public void setCurrentEmployeeId(String currentEmployeeId) {
		this.currentEmployeeId = currentEmployeeId;
	}

	public Appointment getSelectedAppointment() {
		return selectedAppointment;
	}

	public void setSelectedAppointment(Appointment selectedAppointment) {
		this.selectedAppointment = selectedAppointment;
	}

	public Integer getSelectedAppointmentId() {
		return selectedAppointmentId;
	}

	public void setSelectedAppointmentId(Integer selectedAppointmentId) {
		this.selectedAppointmentId = selectedAppointmentId;
	}

	public Map<String, String> getSelectedDrugs() {
		return selectedDrugs;
	}

	public void setSelectedDrugs(Map<String, String> selectedDrugs) {
		this.selectedDrugs = selectedDrugs;
	}

	public Set<Prescription> getPrescriptionList() {
		return prescriptionList;
	}

	public void setPrescriptionList(Set<Prescription> prescriptionList) {
		this.prescriptionList = prescriptionList;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map arg0) {
		// TODO Auto-generated method stub

	}

	public String initPatientVisitationListPage() {

		try {
			Integer patientId = Integer.valueOf(this.selectedPatientId);
			PatientDAOImpl pDao = new PatientDAOImpl();
			Patient patient = pDao.getPatientById(patientId);
			
//			Patient patient = (Patient) session.get("currentUser");

			VisitationUtil vu = new VisitationUtilImpl();
			List<Visitation> visitationList = new ArrayList<Visitation>();
			visitationList = vu.getVisitationByPatient(patient);
			VisitationContentUtil vcu = new VisitationContentUtilImpl();
			retrievedVisitationContents = vcu
					.getLatestVisitationContentListByVisitationList(visitationList);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		session.put("retrievedVisitationContents", retrievedVisitationContents);
		this.actionMode = "showPatient";

		return SUCCESS;
	}

	public String initPatientVisitationListPageByPatientId() {
		try {

			Patient patient = new Patient();
			PatientDAO patientDAO = new PatientDAOImpl();
			patient = patientDAO.getPatientById(selectedPatientId);
			VisitationUtil vu = new VisitationUtilImpl();
			List<Visitation> visitationList = new ArrayList<Visitation>();
			visitationList = vu.getVisitationByPatient(patient);
			VisitationContentUtil vcu = new VisitationContentUtilImpl();
			retrievedVisitationContents = vcu
					.getLatestVisitationContentListByVisitationList(visitationList);
			selectedPatient = patient;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		session.put("retrievedVisitationContents", retrievedVisitationContents);
		this.actionMode = "showPatient";

		return SUCCESS;

	}

	public String initDoctorVisitationListPage() {
		// EmployeeDAOImpl eDao = new EmployeeDAOImpl();
		try {
			EmployeeDAOImpl eDao = new EmployeeDAOImpl();
			Integer doctorId = Integer.valueOf(this.currentEmployeeId);
			Employee doctor = eDao.getEmployeeById(doctorId);
			
			VisitationUtil vu = new VisitationUtilImpl();
			List<Visitation> visitationList = new ArrayList<Visitation>();
			visitationList = vu.getVisitationByDoctor(doctor);
			VisitationContentUtil vcu = new VisitationContentUtilImpl();
			retrievedVisitationContents = vcu
					.getLatestVisitationContentListByVisitationList(visitationList);

			DrugDAO drugDAO = new DrugDAOImpl();
			retrievedDrugs = drugDAO.getAllDrugs();
			session.put("retrievedDrug", retrievedDrugs);
			session.put("retrievedVisitationContents",
					retrievedVisitationContents);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		this.actionMode = "showAll";
		return SUCCESS;
	}

	public String initVisitationViewPage() {
		session.put("triggeredAction", "initVisitationViewPage");
		Integer visitationContentId = Integer
				.valueOf(this.selectedVisitationContentId);
		VisitationContentDAO vcd = new VisitationContentDAOImpl();

		try {
			this.retrievedVisitationContent = vcd
					.getVisitationContentById(visitationContentId);
			session.put("retrievedVisitationContent",
					retrievedVisitationContent);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		session.put("actionResult","");
		return SUCCESS;
	}

	/*
	 * @param String selectedAppointmentId
	 * 
	 * @return List<Drug> retrievedDrugs //all drugList Appointment
	 * selectedAppointment
	 */
	public String initVisitationCreatePage() {
		session.put("triggeredAction", "initVisitationCreatePage");
		try {
			// this.selectedDrugs = new HashMap<String, String>();
			AppointmentDAO appointmentDAO = new AppointmentDAOImpl();
			selectedAppointment = appointmentDAO
					.getAppointmentById(selectedAppointmentId);
			session.put("selectedAppointment", selectedAppointment);
			DrugDAO drugDAO = new DrugDAOImpl();
			retrievedDrugs = drugDAO.getAllDrugs();
			session.put("retrievedDrug", retrievedDrugs);

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		session.put("actionResult","");
		return SUCCESS;
	}

	public String initVisitationUpdatePage() {
		session.put("triggeredAction", "initVisitationUpdatePage");
		Integer vcId = Integer.valueOf(this.selectedVisitationContentId);

		retrievedDrugs = new ArrayList<Drug>();
		dosage = new ArrayList<String>();

		this.selectedDrugs = new HashMap<String, String>();

		try {
			VisitationContentDAO vcd = new VisitationContentDAOImpl();
			this.retrievedVisitationContent = vcd
					.getVisitationContentById(vcId);

			for (Prescription tempPrescription : this.retrievedVisitationContent
					.getPrescriptionSet()) {
				// retrievedDrugs.add(tempPrescription.getDrug());
				// dosage.add(tempPrescription.getDosage());
				this.selectedDrugs.put(
						String.valueOf(tempPrescription.getDrugId()),
						tempPrescription.getDosage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

		DrugDAOImpl drugDao = new DrugDAOImpl();
		try {
			this.retrievedDrugs = drugDao.getAllDrugs();
			session.put("retrievedDrugs", retrievedDrugs);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}

		session.put("retrievedVisitationContent",
				this.retrievedVisitationContent);
		session.put("actionResult","");
		return SUCCESS;
	}

	public String initVisitationStatPage() {
		try {
			EmployeeDAO ed = new EmployeeDAOImpl();
			doctorList = new ArrayList<Employee>();
			doctorList = ed.getAllEmployee("DOCTOR");
			session.put("doctorList", doctorList);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * @param String retrievedVisitationContent.visitationContentId //referenced
	 * appointmentId String retrievedVisitationContent.diagnosis String
	 * retrievedVisitationContent.treatment String
	 * retrievedVisitationContent.comment List<Drugs> selectedDrugs
	 * 
	 * @return VisitationContent retrievedVisitationContent //new created
	 * visitationContent
	 */
	public String createVisitation() {
		session.put("triggeredAction", "createVisitation");
		prescriptionList = new HashSet<Prescription>();

		try {

			VisitationContent createVisitationContent = new VisitationContent();
			createVisitationContent = retrievedVisitationContent;
			Drug drug = new Drug();
			DrugDAO drugDAO = new DrugDAOImpl();

			if ((this.drugId != null) && (!this.drugId.isEmpty())) {
				for (int i = 0; i < this.drugId.size(); i++) {
					drug = drugDAO.getDrugById(Integer.valueOf(drugId.get(i)));
					Prescription prescription = new Prescription();
					prescription.setDrugId(Integer.valueOf(drugId.get(i)));
					prescription.setDrug(drug);
					prescription.setDosage(dosage.get(i));
					boolean drugExisted = false;
					Set<Prescription> tempPrescriptionSet = new HashSet<Prescription>();
					for (Prescription tempPrescription : createVisitationContent
							.getPrescriptionSet()) {
						tempPrescriptionSet.add(tempPrescription);
					}

					for (Prescription tempPrescription : tempPrescriptionSet) {
						if (tempPrescription.getDrug().getDrugId() == prescription
								.getDrug().getDrugId()) {
							createVisitationContent.getPrescriptionSet()
									.remove(tempPrescription);
							createVisitationContent.getPrescriptionSet().add(
									prescription);
							drugExisted = true;
						}
					}
					if (!drugExisted) {
						createVisitationContent.getPrescriptionSet().add(
								prescription);
					}
				}
			}
			// add the prescription list to retrievedVisitationContent

			VisitationContentUtil visitationContentUtil = new VisitationContentUtilImpl();
			Appointment currentAppointment = (Appointment) session
					.get("selectedAppointment");
			Integer visitationContentId;
			visitationContentId = visitationContentUtil
					.createVisitationContentFromAppointment(currentAppointment,
							createVisitationContent);
			VisitationContentDAO visitationContentDAO = new VisitationContentDAOImpl();
			VisitationContent retrievedVisitationContent = new VisitationContent();
			retrievedVisitationContent = visitationContentDAO
					.getVisitationContentById(visitationContentId);

			session.put("retrievedVisitationContent",
					retrievedVisitationContent);

		} catch (Exception e) {
			e.printStackTrace();
			session.put("actionResult","Cannot create visitation !!");
			return ERROR;
		}
		session.put("actionResult","Visitation successfully created !!");
		return SUCCESS;
	}

	/*
	 * @param same as create visitation
	 * 
	 * @return VisitationContent retrievedVisitationContent //new updated
	 * visitationContent
	 */
	public String updateVisitation() {
		session.put("triggeredAction", "updateVisitation");
		prescriptionList = new HashSet<Prescription>();

		try {

			VisitationContent updateVisitationContent = new VisitationContent();
			updateVisitationContent = (VisitationContent) session
					.get("retrievedVisitationContent");
			updateVisitationContent.getPrescriptionSet().clear();
			Drug drug = new Drug();
			DrugDAO drugDAO = new DrugDAOImpl();
			if ((this.drugId != null) && (!this.drugId.isEmpty())) {
				for (int i = 0; i < this.drugId.size(); i++) {
					drug = drugDAO.getDrugById(Integer.valueOf(drugId.get(i)));
					Prescription prescription = new Prescription();
					prescription.setDrugId(Integer.valueOf(drugId.get(i)));
					prescription.setDrug(drug);
					prescription.setDosage(dosage.get(i));
					boolean drugExisted = false;
					Set<Prescription> tempPrescriptionSet = new HashSet<Prescription>();
					for (Prescription tempPrescription : updateVisitationContent
							.getPrescriptionSet()) {
						tempPrescriptionSet.add(tempPrescription);
					}

					for (Prescription tempPrescription : tempPrescriptionSet) {
						if (tempPrescription.getDrug().getDrugId() == prescription
								.getDrug().getDrugId()) {
							updateVisitationContent.getPrescriptionSet()
									.remove(tempPrescription);
							updateVisitationContent.getPrescriptionSet().add(
									prescription);
							drugExisted = true;
						}
					}
					if (!drugExisted) {
						updateVisitationContent.getPrescriptionSet().add(
								prescription);
					}
				}
			}
			// add the prescription list to retrievedVisitationContent
			updateVisitationContent.setComment(retrievedVisitationContent
					.getComment());
			updateVisitationContent.setDiagnosis(retrievedVisitationContent
					.getDiagnosis());
			updateVisitationContent.setTreatment(retrievedVisitationContent
					.getTreatment());
			VisitationContentUtil visitationContentUtil = new VisitationContentUtilImpl();
			Integer visitationContentId;
			visitationContentId = visitationContentUtil
					.updateVisitationContent(updateVisitationContent);
			VisitationContentDAO visitationContentDAO = new VisitationContentDAOImpl();
			retrievedVisitationContent = visitationContentDAO
					.getVisitationContentById(visitationContentId);
			session.put("retrievedVisitationContent",
					retrievedVisitationContent);

		} catch (Exception e) {
			e.printStackTrace();
			retrievedVisitationContent = (VisitationContent) session
					.get("retrievedVisitationContent");
			session.put("actionResult","Cannot update visitation !!");
			return ERROR;
		}
		session.put("actionResult","Visitation successfully updated !!");
		return SUCCESS;
	}

	public String searchVisitation() {
		session.put("triggeredAction", "serachVisitation");
		Integer attributeType = Integer.valueOf(searchType);
		String keyword = searchContent;
		Employee doctor = (Employee) session.get("currentUser");
		try {
			VisitationContentUtil vcu = new VisitationContentUtilImpl();
			retrievedVisitationContents = vcu
					.getLatestVisitationContentByKeywordForDoctor(doctor,
							keyword, attributeType);
			DrugDAO drugDAO = new DrugDAOImpl();
			retrievedDrugs = drugDAO.getAllDrugs();
			session.put("retrievedDrug", retrievedDrugs);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		actionMode = "showAll";
		return SUCCESS;
	}

	public String searchVisitationByDrug() {
		Drug drug = new Drug();
		try {
			DrugDAO drugDAO = new DrugDAOImpl();
			drug = drugDAO.getDrugById(searchDrugId);
			Employee doctor = (Employee) session.get("currentUser");
			VisitationContentUtil vcu = new VisitationContentUtilImpl();
			retrievedVisitationContents = vcu
					.getLatestVisitationContentByDrugForDoctor(drug, doctor);
			retrievedDrugs = drugDAO.getAllDrugs();
			session.put("retrievedDrug", retrievedDrugs);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		actionMode = "showAll";
		return SUCCESS;

	}

	public String searchVisitationByTimePeriod() {
		boolean successFlag = false;
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date startUtilDate = new java.util.Date();
		java.util.Date endUtilDate = new java.util.Date();
		Employee doctor = (Employee)session.get("currentUser");
		System.out.println(doctor.getId());
		try {
			startUtilDate = formatter.parse(startDate);
			endUtilDate = formatter.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date startSQLDate = new java.sql.Date(startUtilDate.getTime());
		Date endSQLDate = new java.sql.Date(endUtilDate.getTime());

		if (endSQLDate.after(startSQLDate) || endSQLDate.equals(startUtilDate)) {
			try {
				AppointmentDAO appointmentDAO = new AppointmentDAOImpl();
				List<Appointment> visitedAppointmentList = appointmentDAO.getVisitedAppointmentByDoctorAndTimePeriod(startSQLDate, endSQLDate, doctor);
						
//						.getVisitedAppointmentByTimePeriod(startSQLDate,
//								endSQLDate);
				// construct a visitation list
				List<Visitation> visitationList = new ArrayList<Visitation>();
				VisitationDAO visitationDAO = new VisitationDAOImpl();
				for (Appointment tempapp : visitedAppointmentList) {
					visitationList.add(visitationDAO
							.getCurrentVersionOfVisitation(tempapp
									.getAppointmentId()));
				}
				// retrieve visitationcontent list
				VisitationContentUtil vcu = new VisitationContentUtilImpl();
				retrievedVisitationContents = vcu
						.getLatestVisitationContentListByVisitationList(visitationList);
				DrugDAO drugDAO = new DrugDAOImpl();
				retrievedDrugs = drugDAO.getAllDrugs();
				session.put("retrievedDrug", retrievedDrugs);
				successFlag = true;
			} catch (Exception e) {
				e.printStackTrace();
				successFlag = false;
			}

		}
		if (successFlag) {
			actionMode = "showAll";
			return SUCCESS;
		} else {
			actionMode = "showAll";
			return ERROR;
		}
	}

	public String searchAppointmentByDoctorAndTimePeriod() {
		boolean successFlag = false;
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date startUtilDate = new java.util.Date();
		java.util.Date endUtilDate = new java.util.Date();
		try {

			startUtilDate = formatter.parse(startDate);
			endUtilDate = formatter.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date startSQLDate = new java.sql.Date(startUtilDate.getTime());
		Date endSQLDate = new java.sql.Date(endUtilDate.getTime());
		if ((endSQLDate.after(startSQLDate) || endSQLDate.equals(startUtilDate))
				&& (searchDoctorId != -1)) {
			try {
				Employee doctor = new Employee();
				EmployeeDAO ed = new EmployeeDAOImpl();
				doctor = ed.getEmployeeById(searchDoctorId);
				AppointmentDAO ad = new AppointmentDAOImpl();
				List<Appointment> appList = new ArrayList<Appointment>();
				appList = ad.getVisitedAppointmentByDoctorAndTimePeriod(
						startSQLDate, endSQLDate, doctor);
				patientStatMap = new HashMap<Patient, Integer>();
				for (Appointment tempApp : appList) {
					if (!patientStatMap.containsKey(tempApp.getPatient())) {
						patientStatMap.put(tempApp.getPatient(), 1);
					} else {
						Integer count = patientStatMap
								.get(tempApp.getPatient()) + 1;
						patientStatMap.put(tempApp.getPatient(), count);
					}
				}

				patientCount = patientStatMap.size();
				displayStartDate = new java.sql.Date(startUtilDate.getTime());
				displayEndDate = new java.sql.Date(endUtilDate.getTime());
				session.put("displayStartDate", displayStartDate);
				session.put("displayEndDate", displayEndDate);
				session.put("searchByDoctorName", doctor.getName());

				successFlag = true;
			} catch (Exception e) {
				e.printStackTrace();
				successFlag = false;
			}
		}
		
		
		if (successFlag) {
			actionMode = "showAll";
			return SUCCESS;
		} else {
			actionMode = "showAll";
			return ERROR;
		}
	}

	public String searchAppointmentByPatientAndTimePeriod() {
		boolean successFlag = false;
		try {
			PatientDAO patientDAO = new PatientDAOImpl();
			Patient patient = new Patient();
			patient = patientDAO.getPatientById(patientId);
			java.sql.Date startDate = (java.sql.Date) session
					.get("displayStartDate");
			java.sql.Date endDate = (java.sql.Date) session
					.get("displayEndDate");
			List<Appointment> appList = new ArrayList<Appointment>();
			AppointmentDAO ad = new AppointmentDAOImpl();
			appList = ad.getVisitedAppointmentByPatientAndTimePeriod(startDate,
					endDate, patient);
			List<Visitation> visitationList = new ArrayList<Visitation>();
			VisitationDAO visitationDAO = new VisitationDAOImpl();
			for (Appointment tempApp : appList) {
				visitationList.add(visitationDAO
						.getCurrentVersionOfVisitation(tempApp
								.getAppointmentId()));
			}

			VisitationContentUtil vcu = new VisitationContentUtilImpl();
			retrievedVisitationContents = vcu
					.getLatestVisitationContentListByVisitationList(visitationList);
			session.put("retrievedVisitationContents",
					retrievedVisitationContents);
			successFlag = true;
		} catch (Exception e) {
			e.printStackTrace();
			successFlag = false;
		}

		if (successFlag) {
			actionMode = "showAll";
			return SUCCESS;
		} else {
			actionMode = "showAll";
			return ERROR;
		}
	}

	/*
	 * @param String selectedVisitationId
	 * 
	 * @return List<VisitationContent> retrievedVisitationContents //the history
	 * of a visitation
	 */
	public String initVisitationHistoryPage() {
		session.put("triggeredAction", "initVisitationHistoryPage");
		try {

			Integer visitationId = Integer.valueOf(this.selectedVisitationId);
			VisitationDAO vd = new VisitationDAOImpl();
			Visitation visitation = vd
					.getCurrentVersionOfVisitation(visitationId);
			VisitationContentDAO vcd = new VisitationContentDAOImpl();
			this.retrievedVisitationContents = vcd
					.getVisitationContentByVisitation(visitation);
			this.retrievedVisitationContent = vcd
					.getCurrentVisitationContent(visitation);
			session.put("retrievedVisitationContents",
					retrievedVisitationContents);
			session.put("retrievedVisitationContent",
					retrievedVisitationContent);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

		return SUCCESS;
	}

	public Map<String, String> getSearchTypeMap() {
		Map<String, String> searchTypeMap = new TreeMap<String, String>();
		searchTypeMap.put("0", "Date");
		searchTypeMap.put("1", "Patient Name");
		searchTypeMap.put("2", "Diagnosis");
		searchTypeMap.put("3", "Comment");
		searchTypeMap.put("4", "Prescription");
		searchTypeMap.put("5", "Treatment");
		return searchTypeMap;
	}

	public Integer getSelectedPatientId() {
		return selectedPatientId;
	}

	public void setSelectedPatientId(Integer selectedPatientId) {
		this.selectedPatientId = selectedPatientId;
	}

	private List<VisitationContent> getLastestVisitationContentByPatientId(
			Integer patientId) {
		Patient patient = new Patient();
		PatientDAOImpl pDAO = new PatientDAOImpl();
		try {
			patient = pDAO.getPatientById(patientId);
		} catch (DAOException e) {
			e.printStackTrace();
		}

		VisitationUtilImpl vui = new VisitationUtilImpl();
		VisitationContentUtilImpl vcui = new VisitationContentUtilImpl();
		List<Visitation> visitationList = new ArrayList<Visitation>();
		try {
			visitationList = vui.getVisitationByPatient(patient);
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<VisitationContent> visitationContentList = new ArrayList<VisitationContent>();
		try {
			visitationContentList = vcui
					.getLatestVisitationContentListByVisitationList(visitationList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return visitationContentList;
	}

	private List<VisitationContent> getLastestVisitationContentByEmployeeId(
			Integer employeeId) {
		Employee employee = new Employee();
		EmployeeDAOImpl eDao = new EmployeeDAOImpl();
		try {
			employee = eDao.getEmployeeById(employeeId);
		} catch (DAOException e) {
			e.printStackTrace();
		}

		VisitationUtilImpl vui = new VisitationUtilImpl();
		VisitationContentUtilImpl vcui = new VisitationContentUtilImpl();
		List<Visitation> visitationList = new ArrayList<Visitation>();
		try {
			visitationList = vui.getVisitationByDoctor(employee);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<VisitationContent> visitationContentList = new ArrayList<VisitationContent>();
		try {
			visitationContentList = vcui
					.getLatestVisitationContentListByVisitationList(visitationList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return visitationContentList;
	}

	

}
